//
//  ZXTimer.h
//  CollagePicture
//
//  Created by simon on 2019/6/21.
//  Copyright © 2019 simon. All rights reserved.
//
//  简介：包装NSTimer使得NSTimer只强引用这个对象，而不强引用方法选择器接收对象（控制器）；
//      target强引用属性定时器对象，（runloop强引用定时器，定时器强引用target），会造成循环引用，无法让target释放dealloc；包装之后能打破循环引用，能在外部主动调用移除定时器，让定时器移除强引用对象；

// 2022.09.07 修改注释


#import <Foundation/Foundation.h>
#import "NSTimer+ZXCategory.h"
#import "ZXWeakProxy.h"

NS_ASSUME_NONNULL_BEGIN


@class ZXTimer;

@protocol ZXTimerDelegate <NSObject>

- (void)zxTimerFired:(ZXTimer *)timer;

@end

@interface ZXTimer : NSObject

@property (nonatomic, weak) id<ZXTimerDelegate> timerDelegate;

@property (nonatomic, strong) NSTimer *timer;


/// 调度模式初始化定时器及设置代理方法；初始化ti时间后开始执行定时器，将以默认mode直接添加到当前的runloop中；
/// @param ti 计时器间隔时间间隔的秒数
/// @param delegate 向其发送由aSelector指定的消息触发的代理
/// @param userInfo 为定时器自定义用户信息
/// @param repeats 是否重复；如果repeats =YES，计时器将重复重新安排自己，直到被设置invalidated无效为止。如果repeats=NO，计时器timer将在触发后运行一次就失效。
- (void)scheduledTimerWithTimeInterval:(NSTimeInterval)ti
          delegate:(id<ZXTimerDelegate>)delegate
          userInfo:(nullable id)userInfo
           repeats:(BOOL)repeats;


///  非调度模式初始化定时器及设置代理方法；您必须使用addTimer:forMode:将新的计时器timer添加到运行循环中
/// @param ti ti description
/// @param delegate delegate description
/// @param userInfo userInfo description
/// @param repeats repeats description
- (void)timerWithTimeInterval:(NSTimeInterval)ti
                              delegate:(id<ZXTimerDelegate>)delegate
                              userInfo:(nullable id)userInfo
                               repeats:(BOOL)repeats;

/// 释放当前对象的唯一方法；只有调用当前方法，移除定时器，才能正常释放当前对象;
- (void)invalidate;



/**
 添加定时器到当前runloop中；

 @param mode NSRunLoopModel中的其中一个；
 */
- (void)addTimerForMode:(NSRunLoopMode)mode;


- (void)fire;

@end

NS_ASSUME_NONNULL_END



//例如 获取验证码的控制器页面
/*
#import "ZXTimer.h"
@interface RegisterViewController ()<UITextFieldDelegate,ZXTimerDelegate>

@property (nonatomic, strong) ZXTimer *timer;
@property (nonatomic, assign) NSInteger smsDownSeconds;

@end

- (void)dealloc
{
    [self.timer stopTimer];
}

- (ZXTimer *)timer
{
    if (!_timer) {
        _timer = [[ZXTimer alloc] init];
    }
    return _timer;
}
#pragma mark - 验证码获取成功后执行

- (void)smsCodeRequestSuccess
{
    self.verfiCodeBtn.enabled = NO;
    self.verfiCodeBtn.backgroundColor = [UIColor lightGrayColor];
    self.smsDownSeconds = 60;
    [self.timer scheduledTimerWithTimeInterval:1 delegate:self userInfo:nil repeats:YES];
}


#pragma mark - ZXTimerDelegate
- (void)zxTimerFired:(ZXTimer *)timer
{
    if (self.smsDownSeconds ==0)
    {
        self.verfiCodeBtn.enabled = NO;
        self.verfiCodeBtn.backgroundColor = [UIColor orangeColor];
        [self.verfiCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.timer stopTimer];
    }else
    {
        [self.verfiCodeBtn setTitle:[[NSNumber numberWithInt:(int)self.smsDownSeconds] description] forState:UIControlStateNormal];
        self.smsDownSeconds--;
    }
}
*/
